/*
 * FunctionDefinitionInstruction.cpp
 *
 *  Created on: 22/nov/2010
 *      Author: computer
 */

#include "FunctionDefinitionInstruction.h"
#include "Pattern.h"
#include "TemplatePattern.cpp"

namespace TreeExpression {

FunctionDefinitionInstruction::FunctionDefinitionInstruction(const std::string& _name) : FlowExpression(), Expression_CRTP<FunctionDefinitionInstruction>(), name(_name) { }

FunctionDefinitionInstruction::FunctionDefinitionInstruction(const FunctionDefinitionInstruction& copy) : Expression(copy), FlowExpression(copy), Expression_CRTP<FunctionDefinitionInstruction>(copy), name(copy.name), listPattern(copy.listPattern) { }

FunctionDefinitionInstruction::~FunctionDefinitionInstruction() { }

std::string FunctionDefinitionInstruction::getName() const {
	return name;
}

void FunctionDefinitionInstruction::setName(std::string name) {
	this->name = name;
}

void FunctionDefinitionInstruction::addParameter(const PatternRecognition::Pattern<>& pattern) {
	listPattern.push_back(pattern);
}

void FunctionDefinitionInstruction::setNameLastPattern(const std::string& _name) {
	listPattern.back().setName(_name);//TODO exist last?
}

std::list< PatternRecognition::Pattern<> > FunctionDefinitionInstruction::getParameter() const {
	return listPattern;
}

AutoPointer<Expression> FunctionDefinitionInstruction::eval(CopyPointer<Context> & context) {
	context->addDeclaration(name, this->getSon());//Add the declaration to the context
	return AutoPointer<Expression>(new FunctionDefinitionInstruction(*this));
}

std::string FunctionDefinitionInstruction::toString() const {
	return name;//TODO tostring()
}

}
